import { constantRoutes, asyncRoutes } from "@/router";

const state = {
  routes: constantRoutes //  这里开始获取的是默认静态路由
};
const mutations = {
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes];
  }
};

const actions = {
  //  根据不同的用户筛选不同的动态路由
  filterRoutes({ commit }, menus) {
    const routes = [];
    menus.forEach(key => {
      routes.push(...asyncRoutes.filter(item => item.name === key));
    });
    commit("setRoutes", routes);

    return routes;
  }
};

export default {
  namespaced: true,
  state,
  //   mutations: {
  //     setRoutes(state, newRoutes) {
  //       state.routes = [...constantRoutes, ...newRoutes];
  //     }
  //   },
  mutations,

  actions
};
